package com.mzj.saas.mservice.lockManager.controller;

import com.mzj.saas.commons.ApiData;
import com.mzj.saas.commons.ResultBean;
import com.mzj.saas.mservice.lock.vo.LockStatusVo;
import com.mzj.saas.mservice.lockManager.service.LockManagerService;
import com.mzj.saas.mservice.lockManager.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 智能锁管理接口
 */
@Controller
@RequestMapping("/lockManager")
@Api(tags = "智能锁管理接口")
public class LockManagerController {
    @Autowired
    private LockManagerService lockManagerService;

    /**
     * 智能锁品牌列表
     *
     * @param accessToken
     * @return
     */
    @GetMapping("/lockBrands")
    @ResponseBody
    public ResultBean<List<LockBrandVO>> lockBrands(@RequestHeader String accessToken) {
        return lockManagerService.lockBrands(accessToken);
    }

    /**
     * 搜索房屋
     *
     * @param accessToken
     * @param search
     * @return
     */
    @GetMapping("/searchHouse")
    @ResponseBody
    public ResultBean<Object> searchHouse(@RequestHeader String accessToken,
                                          @RequestParam(value = "search") String search) {
        return lockManagerService.searchHouse(accessToken, search);
    }

    /**
     * 根据条件搜索房屋
     *
     * @param accessToken
     * @param searchCondition
     * @param residenceCommunityId
     * @param buildId
     * @param unitId
     * @param roomNumber
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/searchHousebyCondition")
    @ResponseBody
    public ResultBean<ApiData<HouseVO>> searchHousebyCondition(@RequestHeader String accessToken,
                                                               @RequestParam(required = false) String searchCondition,
                                                               @RequestParam(required = false) String houseContent,
                                                               @RequestParam(required = false) Long provinceId,
                                                               @RequestParam(required = false) Long cityId,
                                                               @RequestParam(required = false) Long districtIdyId,
                                                               @RequestParam(required = false) Long streetId,
                                                               @RequestParam(required = false) String residenceCommunityId,
                                                               @RequestParam(required = false) Long buildId,
                                                               @RequestParam(required = false) Long unitId,
                                                               @RequestParam(required = false) Integer roomNumber,
                                                               @RequestParam(defaultValue = "0") Integer pageNumber,
                                                               @RequestParam(defaultValue = "5") Integer pageSize) {
        return lockManagerService.searchHousebyCondition(accessToken, searchCondition, houseContent, provinceId, cityId, districtIdyId, streetId, residenceCommunityId, buildId, unitId, roomNumber, pageNumber, pageSize);
    }

    /**
     * 智能锁安装
     *
     * @param accessToken
     * @param vo
     * @return
     */
    @PostMapping("/lockSetup")
    @ResponseBody
    public ResultBean<Object> lockSetup(@RequestHeader String accessToken, @RequestBody LockSetupVO vo) {
        return lockManagerService.lockSetup(accessToken, vo);
    }

    /**
     * 定位城市小区列表
     *
     * @param accessToken
     * @param cityName
     * @return
     */
    @GetMapping("/communityList")
    @ResponseBody
    public ResultBean<List<Map<String, Object>>> communityList(@RequestHeader String accessToken,
                                                               @RequestParam(value = "cityName") String cityName) {
        return lockManagerService.communityList(accessToken, cityName);
    }

    /**
     * 智能锁是否已安装
     *
     * @param accessToken
     * @param mac
     * @return
     */
    @GetMapping("/isLockSetup")
    @ResponseBody
    public ResultBean<Object> isLockSetup(@RequestHeader String accessToken, @RequestParam(value = "mac") String mac) {
        return lockManagerService.isLockSetup(accessToken, mac);
    }

    /**
     * 校验转个人申请
     *
     * @param accessToken
     * @param mac
     * @return
     */
    @GetMapping("/verifyApply")
    @ResponseBody
    public ResultBean<Object> verifyApply(@RequestHeader String accessToken, @RequestParam(value = "mac") String mac) {
        return lockManagerService.verifyApply(accessToken, mac);
    }

    /**
     * 转为个人智能锁
     *
     * @param accessToken
     * @param vo
     * @return
     */
    @PostMapping("/toPersonLock")
    @ResponseBody
    public ResultBean<Object> toPersonLock(@RequestHeader String accessToken, @RequestBody ConfirmVO vo) {
        return lockManagerService.toPersonLock(accessToken, vo);
    }

    /**
     * 智能锁列表
     *
     * @param accessToken
     * @param search
     * @param bindType      绑定类型:1单位 2个人
     * @param name
     * @param mac           智能锁mac
     * @param type          锁类型 1、房屋智能锁 2、门禁智能锁
     * @param houseId       房屋id
     * @param communityId   小区
     * @param buildId       楼宇
     * @param unitId        单元
     * @param roomNumber    房号
     * @param signalLevel   信号状态
     * @param onlineStatus  在线状态 1、在线 2、离线
     * @param orgName       产权所有人
     * @param matchUserName 承租人
     * @param oauthNumber   授权人数
     * @param oauthStatus   授权状态:0未授权，1已授权
     * @param mechineStatus 设备状态
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/lockList")
    @ResponseBody
    public ResultBean<ApiData<LockManagerListVO>> lockList(@RequestHeader String accessToken,
                                                           @RequestParam(value = "search", required = false) String search,
                                                           @RequestParam(value = "bindType") Integer bindType,
                                                           @RequestParam(value = "name", required = false) String name,
                                                           @RequestParam(value = "mac", required = false) String mac,
                                                           @RequestParam(value = "type", required = false) Integer type,
                                                           @RequestParam(value = "houseId", required = false) String houseId,
                                                           @RequestParam(value = "communityId", required = false) String communityId,
                                                           @RequestParam(value = "buildId", required = false) Long buildId,
                                                           @RequestParam(value = "unitId", required = false) Long unitId,
                                                           @RequestParam(value = "roomNumber", required = false) String roomNumber,
                                                           @RequestParam(value = "signalLevel", required = false) Integer signalLevel,
                                                           @RequestParam(value = "onlineStatus", required = false) Integer onlineStatus,
                                                           @RequestParam(value = "orgName", required = false) String orgName,
                                                           @RequestParam(value = "matchUserName", required = false) String matchUserName,
                                                           @RequestParam(value = "oauthNumber", required = false) Integer oauthNumber,
                                                           @RequestParam(value = "oauthStatus", required = false) Integer oauthStatus,
                                                           @RequestParam(value = "mechineStatus", required = false) Integer mechineStatus,
                                                           @RequestParam(value = "leaseStatus", required = false) Integer leaseStatus,
                                                           @RequestParam(defaultValue = "0") Integer pageNumber, @RequestParam(defaultValue = "5") Integer pageSize) {
        return lockManagerService.lockList(accessToken, search, bindType, name, mac, type, houseId, communityId,
                buildId, unitId, roomNumber, signalLevel, onlineStatus, orgName, matchUserName, oauthNumber,
                oauthStatus, mechineStatus, leaseStatus, pageNumber, pageSize);
    }


    /**
     * 智能锁首页数据
     */
    @GetMapping("/lockLeaderList")
    @ResponseBody
    @ApiOperation("智能锁首页数据")
    public ResultBean<List<LockStatusVo>> lockLeaderList(@RequestHeader @ApiParam(name = "accessToken", value = "用户登录信息", required = true)
                                                         String accessToken,
                                                         @RequestParam(value = "houseId") @ApiParam(name = "houseId", value = "房屋ID", required = true)
                                                         String houseId) {
        return lockManagerService.lockLeaderList(accessToken, houseId);
    }

    /**
     * 删除智能锁
     *
     * @param accessToken
     * @param lockId
     * @return
     */
    @GetMapping("/deleteLock")
    @ResponseBody
    public ResultBean<Object> deleteLock(@RequestHeader String accessToken,
                                         @RequestParam(value = "lockId") Long lockId) {
        return lockManagerService.deleteLock(accessToken, lockId);
    }

    /**
     * 查看智能锁设置
     *
     * @param accessToken
     * @param lockId
     * @return
     */
    @GetMapping("/findLockOptions")
    @ResponseBody
    public ResultBean<LockHouseSettingVO> findLockOptions(@RequestHeader String accessToken,
                                                          @RequestParam(value = "lockId") Long lockId) {
        return lockManagerService.findLockOptions(accessToken, lockId);
    }

    /**
     * 智能锁设置操作
     *
     * @param accessToken
     * @param lockId
     * @param type
     * @param value
     * @return
     */
    @GetMapping("/lockOptionsSetup")
    @ResponseBody
    public ResultBean<Object> lockOptionsSetup(@RequestHeader String accessToken,
                                               @RequestParam(value = "lockId") Long lockId, @RequestParam(value = "type") Integer type,
                                               @RequestParam(value = "value", required = false) Integer value) {
        return lockManagerService.lockOptionsSetup(accessToken, lockId, type, value);
    }

    /**
     * 查看临时密码
     *
     * @param accessToken
     * @param lockId
     * @return
     */
    @GetMapping("/findTempPwd")
    @ResponseBody
    public ResultBean<TempPwdVO> findTempPwd(@RequestHeader String accessToken,
                                             @RequestParam(value = "lockId") Long lockId) {
        return lockManagerService.findTempPwd(accessToken, lockId);
    }

    /**
     * 设置临时密码
     *
     * @param accessToken
     * @param lockId
     * @return
     */
    @GetMapping("/authTempPwd")
    @ResponseBody
    public ResultBean<Object> authTempPwd(@RequestHeader String accessToken,
                                          @RequestParam(value = "lockId") Long lockId) {
        return lockManagerService.authTempPwd(accessToken, lockId);
    }

    /**
     * 删除临时密码
     *
     * @param accessToken
     * @param lockId
     * @return
     */
    @GetMapping("/deleteTempPwd")
    @ResponseBody
    public ResultBean<Object> deleteTempPwd(@RequestHeader String accessToken,
                                            @RequestParam(value = "lockId") Long lockId) {
        return lockManagerService.deleteTempPwd(accessToken, lockId);
    }

    /**
     * 恢复出厂设置
     *
     * @param accessToken
     * @param vo
     * @return
     */
    @PostMapping("/factoryReset")
    @ResponseBody
    public ResultBean<Object> factoryReset(@RequestHeader String accessToken, @RequestBody FactoryResetVO vo) {
        return lockManagerService.factoryReset(accessToken, vo);
    }

    /**
     * 授权记录列表
     *
     * @param accessToken
     * @param lockId
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/operatorLogList")
    @ResponseBody
    public ResultBean<ApiData<LockOperatorLogListVO>> operatorLogList(@RequestHeader String accessToken,
                                                                      @RequestParam(value = "lockId") Long lockId, @RequestParam(defaultValue = "0") Integer pageNumber,
                                                                      @RequestParam(defaultValue = "5") Integer pageSize) {
        return lockManagerService.operatorLogList(accessToken, lockId, pageNumber, pageSize);
    }

    /**
     * 告警记录
     *
     * @param accessToken
     * @param search
     * @param date
     * @param matchUserName
     * @param communityId
     * @param buildId
     * @param unitId
     * @param roomNumber
     * @param alarmType     告警类型：1低电量告警 2防撬告警 3胁迫告警 4陌生人试开告警
     * @param beginDate
     * @param endDate
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/alarmLogList")
    @ResponseBody
    public ResultBean<ApiData<AlarmLogListVo>> alarmLogList(@RequestHeader String accessToken,
                                                            @RequestParam(value = "search", required = false) String search,
                                                            @RequestParam(value = "date", required = false) String date,
                                                            @RequestParam(value = "matchUserName", required = false) String matchUserName,
                                                            @RequestParam(value = "communityId", required = false) String communityId,
                                                            @RequestParam(value = "buildId", required = false) Long buildId,
                                                            @RequestParam(value = "unitId", required = false) Long unitId,
                                                            @RequestParam(value = "roomNumber", required = false) String roomNumber,
                                                            @RequestParam(value = "alarmType", required = false) Integer alarmType,
                                                            @RequestParam(value = "beginDate", required = false) String beginDate,
                                                            @RequestParam(value = "endDate", required = false) String endDate,
                                                            @RequestParam(defaultValue = "0") Integer pageNumber, @RequestParam(defaultValue = "5") Integer pageSize) {
        return lockManagerService.alarmLogList(accessToken, search, date, matchUserName, communityId, buildId, unitId,
                roomNumber, alarmType, beginDate, endDate, pageNumber, pageSize);
    }

    @GetMapping("/alarmLogList/v2")
    @ResponseBody
    public ResultBean<ApiData<AlarmLogListVo>> alarmLogListV2(@RequestHeader String accessToken,
                                                              @RequestParam(value = "lockId", required = true) Long lockId,
                                                              @RequestParam(value = "alarmType", required = false) Integer alarmType,
                                                              @RequestParam(value = "endDate", required = false) String endDate,
                                                              @RequestParam(defaultValue = "0") Integer pageNumber, @RequestParam(defaultValue = "5") Integer pageSize) {
        return lockManagerService.alarmLogListV2(accessToken, lockId, alarmType, endDate, pageNumber, pageSize);
    }


    /**
     * 未开门查询
     *
     * @param accessToken
     * @param lockId      锁id
     * @param communityId
     * @param buildId
     * @param unitId
     * @param roomNumber
     * @param type        开门类型:1内开门 2外开门
     * @param openDay     未开门天数
     * @param beginDay
     * @param endDay
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/lockUnOpenLog")
    @ResponseBody
    public ResultBean<ApiData<LockUnOpenLogVO>> lockUnOpenLog(@RequestHeader String accessToken,
                                                              @RequestParam(value = "lockId", required = false) Long lockId,
                                                              @RequestParam(value = "communityId", required = false) String communityId,
                                                              @RequestParam(value = "buildId", required = false) Long buildId,
                                                              @RequestParam(value = "unitId", required = false) Long unitId,
                                                              @RequestParam(value = "roomNumber", required = false) String roomNumber,
                                                              @RequestParam(value = "type", required = false) Integer type,
                                                              @RequestParam(value = "openDay", required = false) Integer openDay,
                                                              @RequestParam(value = "beginDay", required = false) Integer beginDay,
                                                              @RequestParam(value = "matchUserName", required = false) String matchUserName,
                                                              @RequestParam(value = "endDay", required = false) Integer endDay,
                                                              @RequestParam(defaultValue = "0") Integer pageNumber, @RequestParam(defaultValue = "5") Integer pageSize) {
        return lockManagerService.lockUnOpenLog(accessToken, lockId, communityId, buildId, unitId, roomNumber, type,
                openDay, beginDay, endDay, pageNumber, pageSize, matchUserName);
    }

    /**
     * 设置个人锁权限开关
     *
     * @param lockId
     * @param type   0关 1开
     * @return
     */
    @GetMapping("/setPersonPermission")
    @ResponseBody
    public ResultBean<Object> setPersonPermission(@RequestParam(value = "lockId") Long lockId,
                                                  @RequestParam(value = "type") Integer type) {
        return lockManagerService.setPersonPermission(lockId, type);
    }

}
